package main

import (
	"log"
	"net"
)

func udpInit() {
	address := net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 6002}
	listener, err := net.ListenUDP("udp", &address)
	if err != nil {
		panic(err)
	}
	defer listener.Close()

	serverAddress := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9002}
	_, err = listener.WriteToUDP([]byte("hello"), &serverAddress)
	if err != nil {
		panic(err)
	}

	buffer := make([]byte, 1024)
	for {
		n, _, err := listener.ReadFromUDP(buffer)
		if err != nil {
			panic(err)
		}

		log.Println("udp 收到数据:", string(buffer[:n]))
	}
}
